import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import type { PluginOption } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'
import WindiCSS from 'vite-plugin-windicss'
import purgeCss from 'vite-plugin-purgecss'
import { sentryVitePlugin } from '@sentry/vite-plugin'
import pak from './package.json'
import { visualizer } from 'rollup-plugin-visualizer'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    vueDevTools(),
    WindiCSS(),
    purgeCss({
      content: ['**/*.vue', '**/*.html'],
      // safelist: [/nihao/],
      safelist: {
        deep: [/\[.*\]/],
      },
      variables: false, // 不清理CSS变量
    }) as PluginOption,
    sentryVitePlugin({
      project: 'simple-vue',
      authToken:
        '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',
      telemetry: false,
      sourcemaps: {
        assets: './dist/assets/**/*.{js,css,html,map}',
      },
      release: {
        name: pak.version,
      },
    }),
    visualizer({
      open: true,
      filename: 'report.html',
    }),
  ],
  worker: {
    format: 'es',
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  build: {
    sourcemap: true, // 开启源码映射
    rollupOptions: {
      output: {
        manualChunks: {
          // 将 Vue 相关的依赖打包到一个单独的文件中
          vue: ['vue', 'vue-router', 'pinia'],
          // 将 WindiCSS 的类名打包到一个单独的文件中
          // exceljs
          exceljs: ['exceljs'],
          pdfjs: ['pdfjs-dist', 'pdfh5'],
        },
        chunkFileNames: 'assets/js/[name]-1[hash].js', // 这个指的是分割出来的代码块的文件名
        entryFileNames: 'assets/js/[name]-2[hash].js', // 入口文件的文件名
        assetFileNames: 'assets/[ext]/[name]-[hash].[ext]', // 其他资源文件的文件名
      },
    },
  },
  server: {
    host: '0.0.0.0',
    port: 9000,
  },
})
